package com.demo.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.controller.FileUploadController;

import net.sf.json.JSON;
import net.sf.json.JSONArray;

/**
 * 以rest方式推送数据-告警
 * 
 * @author 熊雨会
 *
 */
public class HttpSendEvent {

	public String pushEp(List data, String urls) {
		String resl = null;
		try {
			// System.out.println(data);
			// 创建url资源
			// URL url = new URL("http://192.168.234.139:1532/rest/perf");
			URL url = new URL(urls);
			// 建立http连接
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			// 设置允许输出
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 设置不用缓存
			conn.setUseCaches(false);
			// 设置传递方式
			conn.setRequestMethod("POST");
			// 设置维持长连接
			conn.setRequestProperty("Connection", "Keep-Alive");
			// 设置文件字符集:
			conn.setRequestProperty("Charset", "UTF-8");
			// 转换为字节数组
			byte[] data1 = (data.toString().getBytes());
			// 设置文件长度
			conn.setRequestProperty("Content-Length", String.valueOf(data1.length));
			// 设置文件类型:
			conn.setRequestProperty("contentType", "application/json");

			// 开始连接请求
			conn.connect();
			OutputStream out = conn.getOutputStream();
			// 写入请求的字符串
			out.write(data.toString().getBytes("utf-8"));
			out.flush();
			out.close();

			// 请求返回的状态
			if (conn.getResponseCode() == 200) {
				System.out.println("状态:连接成功");
				// 请求返回的数据
				InputStream in = conn.getInputStream();
				try {
					byte[] data2 = new byte[in.available()];
					in.read(data2);
					// 转成字符串
					resl = new String(data2);

					System.out.println("返回数据:" + resl);

				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} else {
				System.out.println("状态:连接失败，请确认服务路径");
			}

		} catch (Exception e) {
			System.out.println("状态:连接失败，请确认服务路径");
		}
		return resl;
	}

}